<?php
require_once("../DAO/UserDAO.php");
include_once("../DAO/QuestionDAO.php");
include_once("../Entities/Question.php");
include_once("../DAO/AnswerDAO.php");
include_once("../DAO/ParagraphDAO.php");
include_once("../DAO/NoteDAO.php");
include_once("../Entities/Answer.php");
include_once("../Entities/Test.php");
include_once("../Entities/TestDetail.php");
include_once("../Utility/WebUtilities.php");
include_once("../Utility/WebSession.php");

$questionDAO = new QuestionDAO();
$answerDAO = new AnswerDAO();
$test = WebSession::GetSession("TestSession");
if($test == null)
{
	WebUtilities::Redirect("MainTest.php");
}
$currentQuestion = WebSession::GetSession("CurrentQuestion");
$question = $questionDAO->GetQuestionById($test->Details[$currentQuestion]->QuestionId);
if($question == null)
{
	WebUtilities::Redirect("MainTest.php");
}
$answers = $answerDAO->GetAnswersByQuestionId($question->Id);
$paragraphStr = "";
if($question->Paragraph != null)
{
	$paragraphDAO = new ParagraphDAO();
	$paragraph = $paragraphDAO->GetParagraphById($question->Paragraph);
	if($paragraph !=null)
		$paragraphStr = $paragraph->Content;
}
$answerStr = "";
$strRights = "";
for($i = 0 ; $i < count($answers); $i++)
{
	if($answers[$i]->IsCorrect == 1)
		$strRights = $strRights . $answers[$i]->Id . "_";
	$answerStr = $answerStr . "<input type='radio' name='inputAnswers' value='" . $answers[$i]->Id . "'>" . $answers[$i]->Content . "</input><br/>";
}
if($strRights != "")
{
	$strRights = substr($strRights, 0, strlen($strRights) - 1);
}
$nextQuestion = ($currentQuestion + 1) >= count($test->Details) ? -1 : $test->Details[$currentQuestion + 1]->QuestionId;
$prevQuestion = ($currentQuestion - 1) < 0 ? -1 : $test->Details[$currentQuestion - 1]->QuestionId;
$strTime = "";
$strViewAnswer = "";
if(WebSession::GetSession("TestType") == 0)
{
	$strViewAnswer = "<input type='button' value='View Answer' onclick='ViewAnswers()' />";
}
else
{
	$strTime = "<label class='timer'>" . WebUtilities::GetTimeTest() . ":00</label>";
	$strRights = "";
	$bodyEvents = "onload='CountDown(" . WebUtilities::GetTimeTest() .", 0)'";
}
$controlStr = "";
$strNote = "";
if(WebSession::CurrentUserNoReturn() != null)
{
	$controlStr = "<a onclick='openCloseWordPanel()'><img src='../images/dictionary.png' /><label>Words</label></a>
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a onclick='openCloseNotePanel()'><img src='../images/notes.png' /><label>Notes</label></a>
					&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
					<a onclick=\"window.open('history.php')\"><img src='../images/history.png' /><label>Histories</label></a>
					<input type='hidden' id='hdCurrentPanel' value='-1'/> ";
	$noteDAO = new NoteDAO();
	$noteList = $noteDAO->GetNotesByUser(WebSession::CurrentUserNoReturn()->UserId);
	if(count($noteList) > 0)
		$strNote = $noteList[0]->Content;
}
$mainContent = "<div class='userControlPanel'>" . $controlStr
					 . $strTime .
				"</div>
		 		<div class='wordPanel'>
					<div class='middleWord'>
						<a href='#' onclick=\"window.open('http://vdict.com/')\">Vdict - Từ điển online</a><br/>
						<input id='tbKey' style='width:150px;' /><br/>
						<input type='radio' checked name='rbType1' value='0' id='rbViewEV'>English - Vietnamese</input><br/>
						<input type='radio' name='rbType1' value='1' id='rbViewVE'>Vietnamese - English</input><br/><br/>
						<input type='button' value='Search' onclick='GetWord()' />
						<input type='button' value='Add' onclick='ShowPanel(3)'/>
					</div>
					<textarea rows='2' cols='20' class='result' id='tbViewWord'>
					</textarea>
		 		</div>
				<div class='notePanel'>
					<textarea rows='2' cols='20' id='tbViewNote' class='transparent'>" . $strNote .
					"</textarea>
					<div style='margin-left:50px;'>
						<input type='button' value='<<' onclick='GetNote(-1)'/>
						<input type='button' value='Add' onclick='ShowPanel(2)'/>
						<input type='button' value='>>' onclick='GetNote(1)'/>
					</div>
					<input type='hidden' id='hdCurrentIndexNote' value='0' />
				</div>
				<div class='middleContent'>
					<input type='hidden' id='hdTypeTest' value='" . WebSession::GetSession("TestType") . "' />
					<input type='hidden' id='hdCurrentIndex' value='" . $currentQuestion . "' />
					<input type='hidden' id='hdCurrentQuestion' value='" . $question->Id . "' />
					<input type='hidden' id='hdRights' value='" . $strRights . "'/>
					<div class='paragraphArea'>"
					. $paragraphStr .	
					"</div><br/>
					<div class='questionArea'>"
					 . ($currentQuestion + 1) . ". " . $question->Content .	
					"</div></br><br/>
					<div class='answerArea'>"
					. $answerStr .
					"</div>
					<br/>
					<div>
						<input type='button' id='btPrev' onclick='ChangeQuestion(this, -1)' title='" . $prevQuestion. "' value='<< Prev' " . (($currentQuestion - 1) < 0 ? "disabled" : "")  . "' />
						" . $strViewAnswer . "
						<input type='button' id='btNext' onclick='ChangeQuestion(this, 1)' value='Next >>' title='" . $nextQuestion . "' " . (($currentQuestion + 1) >= count($test->Details) ? "disabled" : "")  . "' />
						<input type='button' id='btFinish' value='Finish' onclick='FinishTest()' />
						<input type='button' id='btFeedback' value='Feedback this question' onclick='ShowPanel(1)'/>
					</div>
				</div>
				<div id='popupPanel' class='popupPanel'>
					<a href='#' onclick='ClosePopupPanel(); return false;'>Close</a><br/>
					<div id='panelContent' style='padding:10px 10px 10px 10px;overflow:auto;'>
						<textarea id='tbFeedback' rows='2' cols='20' style='width:400px;height:200px'></textarea>
						<div>
							<input type='button' value='Feedback' onclick='SendFeedback()' />
						</div>
					</div>
					<div id='panelContentNote' style='padding:10px 10px 10px 10px;overflow:auto;'>
						<textarea id='tbNote' rows='2' cols='20' style='width:400px;height:200px'></textarea>
						<div>
							<input type='button' value='Add Note' onclick='SendNote()' />
						</div>
					</div>
					<div id='panelContentWord' style='padding:10px 10px 10px 10px;overflow:auto;'>
						<div class='oneLine'>
							<label class='left1'>Key</label>
							<input id='tbAddKey' style='width:300px'/>
						</div>
						<div class='oneLine'>
							<div class='left1'>Type</div>
							<input type='radio' checked name='rbType2' value='0' id='rbEV'>English-Vietnamese</input>
							<input type='radio' name='rbType2' value='1' id='rbVE'>Vietnamese-English</input>
						</div>
						<div class='oneLine'>
							<div class='left1'>Translate</div>
							<textarea id='tbTranslate' rows='2' cols='20' style='width:300px;height:100px'></textarea>
						</div>
						<div>
							&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
							<input type='button' value='Add Word' onclick='SendWord()' />
						</div>
					</div>
				</div>";
include("MasterPage.php");

?>